package com.wavehi.test;

import com.waveh.mapper.IUserDynamicSQLMapper;
import com.wavehi.pojo.User;
import com.wavehi.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

/**
 * @Author WaveHi
 * @Date 25/9/2021 下午 9:10
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Demonstrate the use of level 01 cache.
 */
public class Level01CacheDemo {

    /**
     * 同一次SqlSession查询的数据会放到本地缓存中
     * 当需要再次获取相同的数据时会直接从缓存中获取，而不会再次向数据库发送sql语句进行查询
     */
    @Test
    public void useCacheOfLevel01() {
        SqlSession session = null;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserDynamicSQLMapper mapper = session.getMapper(IUserDynamicSQLMapper.class);
            //查询
            User user01 = mapper.selectById(1L);
            User user02 = mapper.selectById(1L);
            //测试
            boolean isTheSameObject = user01 == user02;
            if(isTheSameObject) {
                System.out.println("User01 and user02 is the same object.");
            }
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
        }
    }

    /**
     * 不同的SqlSession具有不同的一级缓存，不共享数据
     */
    @Test
    public void invalidateOfCache01() {
        SqlSession session01 = null;
        SqlSession session02 = null;
        try {
            //获取数据库会话
            session01 = MyBatisUtils.getSession();
            session02 = MyBatisUtils.getSession();
            //获取代理对象
            IUserDynamicSQLMapper mapper01 = session01.getMapper(IUserDynamicSQLMapper.class);
            IUserDynamicSQLMapper mapper02 = session02.getMapper(IUserDynamicSQLMapper.class);
            //查询
            User user01 = mapper01.selectById(1L);
            User user02 = mapper02.selectById(1L);
            //测试
            boolean isTheSameObject = user01 == user02;
            if(isTheSameObject) {
                System.out.println("User01 and user02 is the same object");
            }else {
                System.out.println("User01 and user02 is not the same object.");
            }
        } finally {
            //资源的关闭
            if(session01 != null) {
                session02.close();
                session01 = null;
            }
            if(session02 != null) {
                session02.close();
                session02 = null;
            }
        }
    }

    /**
     * 同一SqlSession，不同的查询条件。
     */
    @Test
    public void invalidationOfCache02() {
        SqlSession session = null;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserDynamicSQLMapper mapper = session.getMapper(IUserDynamicSQLMapper.class);
            //查询
            User user01 = mapper.selectById(1L);
            User user02 = mapper.selectByUsername("com.com.wavehi");
            //测试
            boolean isTheSameObject = user01 == user02;
            if(isTheSameObject) {
                System.out.println("User01 and User02 is the same object.");
            }else {
                System.out.println("User01 and user02 is not the same object.");
            }
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
        }
    }

    /**
     * SqlSession相同，两次查询之间进行了增删改操作，导致数据库数据发生变化。
     */
    @Test
    public void invalidationOfCache03() {
        SqlSession session = null;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserDynamicSQLMapper mapper = session.getMapper(IUserDynamicSQLMapper.class);
            //查询和添加
            User user01 = mapper.selectById(1L);
            mapper.insert(new User("Honny", "qwerty"));
            session.commit();
            User user02 = mapper.selectById(1L);
            //测试
            boolean isTheSameObject = user01 == user02;
            if(isTheSameObject) {
                System.out.println("User01 and user02 is the same object.");
            }else {
                System.out.println("User01 and user02 is not the same object.");
            }
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
        }
    }

    /**
     * 同一个SqlSession，手动清除了一级缓存。
     */
    @Test
    public void invalidationOfCache04() {
        SqlSession session = null;
        try {
            //获取数据库会话
            session = MyBatisUtils.getSession();
            //获取代理对象
            IUserDynamicSQLMapper mapper = session.getMapper(IUserDynamicSQLMapper.class);
            //查询
            User user01 = mapper.selectById(1L);
            session.clearCache();
            User user02 = mapper.selectById(1L);
            //测试
            boolean isTheSameObject = user01 == user02;
            if(isTheSameObject) {
                System.out.println("User01 and user02 is the same object.");
            }else {
                System.out.println("User01 and user02 is not the same object.");
            }
        } finally {
            //资源的关闭
            if(session != null) {
                session.close();
                session = null;
            }
        }
    }
}
